﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Business;
using NetDon.OA.Interface.Admin;
using NetDon.OA.Interface.Configuration;
using NetDon.OA.Portal.Constants;
using NineRays.WebControls;
using ServiceContainer = NetDon.Utilities.ServiceContainer;
using NetDon.Utilities;
namespace NetDon.OA.Portal.Components
{
    public partial class PositionControl : System.Web.UI.UserControl
    {
        public event Action<object, EventArgs> LoadItems;
        public Guid PositionId
        {
            get { return ViewState["Id"] != null ? (Guid)ViewState["Id"] : Guid.Empty; }

            set { ViewState["Id"] = value; }
        }
        public string PositionName
        {
            get { return ViewState["PositionName"] != null ? (string)ViewState["PositionName"] : string.Empty; }

            set { ViewState["PositionName"] = value; }
        }

        public Guid? ParentID
        {
            get { return ViewState["ParentID"] != null ? (Guid?)ViewState["ParentID"] : null; }

            set
            {
                ViewState["ParentID"] = value == Guid.Empty ? null : value;
            }
        }

        public PositionNodeType CurrentNodeType
        {
            get { return ViewState["CurrentNodeType"] != null ? (PositionNodeType)ViewState["CurrentNodeType"] : PositionNodeType.Position; }

            set { ViewState["CurrentNodeType"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindPostion();
             
            }
        }

        protected void btnModify_Click(object sender, EventArgs e)
        {
            if(LoadItems!=null)
            {
                PositionId = this.treeCompany.SelectedNode.Value.ToGuid();
                PositionName = this.treeCompany.SelectedNode.Text.Trim();
                LoadItems(null, null);
            }
          

        }
        private void BindPostion()
        {
            var service = ServiceContainer.GetService<IPostService>();
            var rootCategories = service.GetRootPosts();
            treeCompany.Nodes.Clear();
            //add root company
            treeCompany.Nodes.Add(CreateRootNode());
            for (int i = 0; i < rootCategories.Count; i++)
            {
                treeCompany.Nodes[0].ChildNodes.Add(CreatePositionTreeNode(rootCategories[i]));
            }

            treeCompany.Nodes[0].Text =
                Server.HtmlEncode(SystemConfiguration.RootPositionName);
            treeCompany.Nodes[0].PopulateNodesOnDemand = rootCategories.Count > 0 ? true : false;
            treeCompany.SelectedNode = treeCompany.Nodes[0];
            PositionId = Guid.Empty;
            ParentID = null;
            CurrentNodeType = PositionNodeType.Root;

        }

        private FlyTreeNode CreateRootNode()
        {
            return new FlyTreeNode(
                            Server.HtmlEncode(SystemConfiguration.RootPositionName),
                            Guid.Empty.ToString())
            {
                NodeTypeID = NodeType.Root.ToString(),
                ImageUrl = ApplicationImages.TreeNodeClosed,
                SelectedImageUrl = ApplicationImages.TreeNodeOpened,
                Expanded = true,
                ExpandLevel = 99,
                Tag = -1
            };
        }

        protected void treeCompany_PopulateNodes(object sender, FlyTreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count <= 0)
            {
                LoadChildNodes(e.Node);
            }
        }

        private FlyTreeNode CreatePositionTreeNode(PostData postData)
        {
            return new FlyTreeNode(
                            Server.HtmlEncode(postData.PostName),
                            postData.PostId.ToString())
            {
                NodeTypeID = PositionNodeType.Position.ToString(),
                PopulateNodesOnDemand = postData.HasChildren,
                ImageUrl = ApplicationImages.TreeNodeClosed,
                SelectedImageUrl = ApplicationImages.TreeNodeOpened,
                Tag = postData.NodeLevel
            };
        }

        private void LoadChildNodes(FlyTreeNode node)
        {
            if (node.ChildNodes.Count <= 0)
            {
                //load companies
                var service = ServiceContainer.GetService<IPostService>();
                var childCompanies = (PostDataCollection)service.GetPost(node.Value.ToGuid()).GetChildren();
                for (int i = 0; i < childCompanies.Count; i++)
                {
                    node.ChildNodes.Add(CreatePositionTreeNode(childCompanies[i]));

                }


            }
        }

    }
}